/**
 入驻小区
 **/
(function (vc) {
    vc.extends({
        data: {
            goodCashierConfirm: {
                authCode: '',
                preMoney: 0.0,
                money: 0.0,
                bonusPoints: 0.0,
                lastmoney: 0.0,
                productStocks: [],
                vipUser: {},
                discountType: '1001',
                discountRate: '1',
                discountPrice: 0,
                receivableMoney: 0.0,
                givechange: 0,
                hideModal: false,
                tmpParam: {}
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('goodCashierConfirm', 'open', function (_goodCashierInfo) {
                $that.clearConfirm();
                let imount = _goodCashierInfo.vipUser.integralAmount;
                let _bonusPoints = _goodCashierInfo.bonusPoints;
                if (imount < _bonusPoints) {
                    vc.toast('会员积分不足');
                    return;
                }
                if (_bonusPoints == 0 && _goodCashierInfo.money == 0) {
                    vc.toast('请先选择商品');
                    return;
                }
                vc.copyObject(_goodCashierInfo, $that.goodCashierConfirm);
                $that.goodCashierConfirm.preMoney = _goodCashierInfo.money;
                $that.goodCashierConfirm.receivableMoney = _goodCashierInfo.money;
                //$that.goodCashierConfirm.productStocks = $that.goodCashierConfirm.productStocks
                $('#_confirmPayCarshCode').modal('show');
                $that.goodCashierConfirm.lastmoney = $that.goodCashierConfirm.money;
            });
            vc.on('goodCashierConfirm', 'openWxCarsh', function (_goodCashierInfo) {
                $that.clearConfirm();
                let imount = _goodCashierInfo.vipUser.integralAmount;
                let _bonusPoints = _goodCashierInfo.bonusPoints;
                if (imount < _bonusPoints) {
                    vc.toast('会员积分不足');
                    return;
                }
                if (_goodCashierInfo.money == 0) {
                    vc.toast('请先选择商品，只有积分商品使用结算功能！');
                    return;
                }
                vc.copyObject(_goodCashierInfo, $that.goodCashierConfirm);
                $that.goodCashierConfirm.preMoney = _goodCashierInfo.money;
                $that.goodCashierConfirm.receivableMoney = _goodCashierInfo.money;
                $that.goodCashierConfirm.authCode = '';
                $('#_goodCashierWechatAuthCode').modal('show');
                setTimeout('document.getElementById("wxAuthCode").focus()', 1000);
            });
            $('#_goodCashierWechatAuthCode').on('hidden.bs.modal', function () {
                //你的代码
                $that.goodCashierConfirm.hideModal = true;
            })
        },
        methods: {
            /**
             * 微信支付功能
             * @returns
             */
            _doPayWxCarsh: function () {
                if (!$that.goodCashierConfirm.authCode) {
                    vc.toast('请先扫码');
                    return;
                }
                let _storeOrderCarts = [];
                $that.goodCashierConfirm.productStocks.forEach(item => {
                    _storeOrderCarts.push({
                        cateType: item.cateType,
                        productId: item.productId,
                        prodName: item.prodName,
                        valueId: item.valueId,
                        goodsNum: item.count,
                        storeId: item.storeId,
                        shopId: item.shopId,
                        discountType: item.discountType,
                        discountRate: item.discountRate,
                        discountPrice: item.discountPrice,
                        cartType: '3306'
                    })
                });
                let _param = {
                    storeOrder: {
                        personId: '-1',
                        personName: $that.goodCashierConfirm.vipUser.vipName,
                        payPrice: $that.goodCashierConfirm.money,
                        discountType: $that.goodCashierConfirm.discountType,
                        discountRate: $that.goodCashierConfirm.discountRate,
                        discountPrice: $that.goodCashierConfirm.discountPrice
                    },
                    storeOrderCarts: _storeOrderCarts,
                    authCode: $that.goodCashierConfirm.authCode,
                    vipUsers: $that.goodCashierConfirm.vipUser,
                    shopId: vc.getCurrentCommunity().shopId
                }
                vc.http.apiPost(
                    '/payment/goodCashierToPay',
                    JSON.stringify(_param),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.goodCashierConfirm.authCode = '';
                        if (_json.code == 0) {
                            //关闭model
                            $('#_goodCashierWechatAuthCode').modal('hide');
                            vc.toast(_json.msg);
                            vc.emit('goodCashier', 'clearAll', {});
                            $that._openOrderPrint(_json.data);
                            return;
                        } else if (_json.code == 41) {
                            vc.toast(_json.msg);
                            $that.goodCashierConfirm.tmpParam = _json.data;
                            if (!$that.goodCashierConfirm.hideModal) {
                                setTimeout('$that._doCheckPayWxCarsh()', 5000);
                            }
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _doCheckPayWxCarsh: function () {
                vc.http.apiPost(
                    '/payment/checkGoodCashierToPay',
                    JSON.stringify($that.goodCashierConfirm.tmpParam),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.goodCashierConfirm.authCode = '';
                        if (_json.code == 0) {
                            //关闭model
                            $('#_goodCashierWechatAuthCode').modal('hide');
                            vc.toast(_json.msg);
                            vc.emit('goodCashier', 'clearAll', {});
                            $that._openOrderPrint(_json.data);
                            return;
                        } else if (_json.code == 41) {
                            //vc.toast(_json.msg);
                            if (!$that.goodCashierConfirm.hideModal) {
                                setTimeout('$that._doCheckPayWxCarsh()', 5000);
                            }
                        }
                        // vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _closePayCarsh: function () {
                $that.receivable = 0.0;
                $that.discount = 100.0;
                $that.givechange = 0.0;
                $('#_confirmPayCarshCode').modal('hide');
            },
            _payCarsh: function () {
                if ($that.goodCashierConfirm.lastmoney > 0) {
                    if (parseFloat($that.goodCashierConfirm.receivableMoney) < parseFloat($that.goodCashierConfirm.money)) {
                        vc.toast('收款不足');
                        return;
                    }
                }
                if (parseFloat($that.goodCashierConfirm.givechange) < 0) {
                    vc.toast('找零不能为负');
                    return;
                }
                let _storeOrderCarts = [];
                $that.goodCashierConfirm.productStocks.forEach(item => {
                    _storeOrderCarts.push({
                        cateType: item.cateType,
                        productId: item.productId,
                        prodName: item.prodName,
                        valueId: item.valueId,
                        goodsNum: item.count,
                        storeId: item.storeId,
                        shopId: item.shopId,
                        discountType: item.discountType,
                        discountRate: item.discountRate,
                        discountPrice: item.discountPrice,
                        cartType: '3306'
                    })
                });
                let _param = {
                    storeOrder: {
                        personId: '-1',
                        personName: $that.goodCashierConfirm.vipUser.vipName,
                        payPrice: $that.goodCashierConfirm.money,
                        discountType: $that.goodCashierConfirm.discountType,
                        discountRate: $that.goodCashierConfirm.discountRate,
                        discountPrice: $that.goodCashierConfirm.discountPrice
                    },
                    storeOrderCarts: _storeOrderCarts,
                    vipUsers: $that.goodCashierConfirm.vipUser,
                    shopId: vc.getCurrentCommunity().shopId
                }
                vc.http.apiPost(
                    '/payment/goodFaceToFaceCashierToPay',
                    JSON.stringify(_param),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            $that._updateStoreOrderCartUp();
                            //关闭model
                            vc.toast(_json.msg);
                            $that.clearCashier();
                            $('#_confirmPayCarshCode').modal('hide');
                            $that._openOrderPrint(_json.data);
                            vc.toast(_json.msg);
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _openOrderPrint: function (_order) {
                window.open('/smallPrint.html#/pages/goods/printSmallOrder?orderId=' + _order.orderId + "&autoPrint=Y");
            },
            // 计算折扣后金额
            caculateMoney() {
                if (!isNaN($that.discount) && $that.discount > 0 && $that.discount <= 100) {
                    $that.goodCashierInfo.lastmoney = parseFloat($that.goodCashierInfo.money) * parseFloat($that.discount).toFixed(2) / 100;
                } else {
                    $that.goodCashierInfo.lastmoney = $that.goodCashierInfo.money;
                }
                $that.givechange = 0;
                $that.receivable = 0;
            },
            givechangeMoney() {
                if (!isNaN($that.receivable) && $that.receivable.length > 0) {
                    $that.givechange = (parseFloat($that.receivable) - $that.goodCashierInfo.lastmoney).toFixed(2);
                }
            },
            _goodCashierConfirmComputeMoney: function () {
                //金额
                let _preMoney = $that.goodCashierConfirm.preMoney;
                let _discountType = $that.goodCashierConfirm.discountType;
                let _discountRate = $that.goodCashierConfirm.discountRate;
                let _discountPrice = $that.goodCashierConfirm.discountPrice;
                if (_discountType == '1001') {
                    if (_discountPrice > 1) {
                        vc.toast('打折率请输入0-1 数字');
                        return;
                    }
                    _discountPrice = parseFloat(_preMoney) * parseFloat(_discountRate);
                    _discountPrice = parseFloat(_preMoney) - _discountPrice;
                }
                $that.goodCashierConfirm.discountPrice = parseFloat(_discountPrice).toFixed(2);
                $that.goodCashierConfirm.money = (_preMoney - parseFloat(_discountPrice)).toFixed(2);
                $that.goodCashierConfirm.receivableMoney = $that.goodCashierConfirm.money;
            },
            _changeReceivableMoney: function () {
                let _money = $that.goodCashierConfirm.money;
                let _receivableMoney = $that.goodCashierConfirm.receivableMoney;
                $that.goodCashierConfirm.givechange = (parseFloat(_receivableMoney) - parseFloat(_money)).toFixed(2);
            },
            _closeWxPayCarsh: function () {
                $('#_goodCashierWechatAuthCode').modal('hide');
            },
            clearConfirm: function () {
                $that.goodCashierConfirm = {
                    authCode: '',
                    preMoney: 0.0,
                    money: 0.0,
                    bonusPoints: 0.0,
                    lastmoney: 0.0,
                    productStocks: [],
                    vipUser: {},
                    discountType: '1001',
                    discountRate: '1',
                    discountPrice: 0,
                    receivableMoney: 0.0,
                    givechange: 0,
                    hideModal: false,
                    tmpParam: {}
                }
            }
        }
    });
})(window.vc);
